var users = {};
var departments;

//上传模板
layui.use('upload', function () {
    var layer = layui.layer;
    var upload = layui.upload;
    upload.render({
        elem: '#exportInternalManagementItems,#exportExternalManagementItems', //绑定元素
        url: $.MvcSheetUI.PortalRoot + "/ImportHandler/ImportHandler", //上传接口
        accept: 'file',
        exts: 'xls',
        data: {},
        done: function (res) {
            if (res.sucess) {
                // debugger;
                var rows = res.data;
                var id = $(this.item).attr('id');

                var xlsres = res.data; // excel 解析结果数组
                var xlsrow; // 每行数据
                var mycache = []; //ajax 缓存
                var gridDate = [];

                var wlmc;// 物料名称
                for (var j = 0, len = xlsres.length; j < len; j++) {
                    // console.log(xlsres[j]);
                    // "10001;qwe;;;;;;;人力资源总监"

                    xlsrow = xlsres[j].split(";");
                    gridDate[j] = xlsrow;

                    //通过物料名称获取其他值
                    if (xlsrow && (wlmc = xlsrow[1])) {
                        var mydata = {};
                        if (!(mycache && mycache[wlmc])) {

                            var param = '{"mc":"' + wlmc + '"}';
                            mydata = getMyGetGridData(param);


                            /* mydata:
                            {
                                "Total": 1,
                                "Rows": [
                                        {
                                            "ObjectID": "a255f50f-1f08-4247-806c-3b7584c67e5d",
                                            "mc": "物料1",
                                            "khbm": "kh001",
                                            "bm": "w001",
                                            "pmbm": "pm001",
                                            "ppbm": "pp001"
                                        }
                                    ]
                            }
                            */

                            if (mydata && mydata["Total"] > 0)
                                mycache[j] = mydata["Rows"][0]; //放入缓存
                        }
                    }
                }

                //set gridview
                var rowtitle = [
                    ["bm",// 字段名称
                        1], // 字段类型 1，string, 2,参与者
                    ["mc", 1], ["khbm", 1], ["khmc", 1], ["ppbm", 1], ["ppmc", 1], ["pric", 1], ["num", 1], ["pmbm", 1], ["pmmc", 1], ["txsh", 2]];
                setMyGridView("bjxq", rowtitle, gridDate, mycache);


                layer.msg('导入成功', {time: 1000});
            } else {
                layer.msg('上传失败');
            }
        },
        error: function () {
            //请求异常回调
            layer.msg('请求异常回调');
        }
    });
});

function setMyGridView(gridfield, title, xlsdata, mycache) {
    if (xlsdata) {
        var rowName = gridfield + ".";
        var grid = $.MvcSheetUI.GetSheetDataItem(gridfield, 0);
        var gridNew = $("table[data-datafield=" + gridfield + "]").SheetGridView(grid);
        gridNew._Clear();
        // debugger;

        for (var i = 0; i < xlsdata.length; i++) {
            gridNew._AddRow();

            var rowdata = xlsdata[i];//每行的值
            var rowcache = mycache[i];

            var celvalue;
            var gridIndex = i + 1;
            for (var j = 0; j < title.length; j++) {// 给行中的每个表单添加值
                // debugger;
                celvalue = "";
                if ((mycache && rowcache[title[j][0]]) || title[j][1] == 2) {
                    if (title[j][1] == 1) //string
                        celvalue = rowcache[title[j][0]];
                    else if (title[j][1] == 2) {// 参与者
                        getUsers();

                        // 其他类型值预留
                        debugger;
                        if (users) {

                            celvalue = users[rowcache[title[8][0]]].Id;


                        }


                    } else
                        continue;
                } else {
                    celvalue = rowdata[j];
                }
                if (celvalue) {

                    $.MvcSheetUI.SetControlValue(rowName + title[j][0], celvalue, gridIndex);
                }

            }


        }


    }
}
    function getMyGetGridData(param) {
        var result = null;
        //http://localhost:8081/Portal/RunBizQuery/GetGridData
        // schemaCode: slwl
        // queryCode: 商洛物料查询
        // FilterStr: {"mc":"物料1"}
        // PageIndex: 1
        // PageSize: 20

        $.ajax({
            type: "POST",
            url: "/Portal/RunBizQuery/GetGridData/",
            data: {
                "schemaCode": "slwl",
                "queryCode": "商洛物料查询",
                "FilterStr": param,
                "PageIndex": 1,
                "PageSize": 20
            },
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;

                return result;
            }
        })
        return result;
    }


    function getMyGetGridData(param) {
        var result = null;
        //http://localhost:8081/Portal/RunBizQuery/GetGridData
        // schemaCode: slwlcxt
        // queryCode: wulcxxq
        // FilterStr: {"mc":"物料1"}
        // PageIndex: 1
        // PageSize: 20

        $.ajax({
            type: "POST",
            url: "/Portal/RunBizQuery/GetGridData/",
            data: {
                "schemaCode": "slwlcxt",
                "queryCode": "wulcxxq",
                "FilterStr": param,
                "PageIndex": 1,
                "PageSize": 20
            },
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;
                return result;
            }
        })
        return result;
    }


    function getGridData(schemaCode, queryCode, FilterStr, PageIndex, PageSize) {
        var result = null;
        //http://localhost:8081/Portal/RunBizQuery/GetGridData
        // schemaCode: slwl
        // queryCode: 商洛物料查询
        // FilterStr: {"mc":"物料1"}
        // PageIndex: 1
        // PageSize: 20

        $.ajax({
            type: "POST",
            url: "/Portal/RunBizQuery/GetGridData/",
            data: {
                "schemaCode": schemaCode,
                "queryCode": queryCode,
                "FilterStr": FilterStr,
                "PageIndex": PageIndex,
                "PageSize": PageSize
            },
            dataType: "json",
            async: false,
            success: function (data) {
                result = data;
                return result;
            }
        })
        return result;
    }


    function getUsers() {
        //获取users
        //zuser,cxhyzz,1,20
        debugger;
        if (JSON.stringify(users) == "{}") {
            var result = getGridData("zuser", "cxhyzz", null, 1, 200);

            if (result && result.Total > 0) {
                debugger;
                for (var i = 0; i < result.Rows.length; i++) {
                    users[result.Rows[i].Code] = result.Rows[i];
                }
            }
        }

    }




